/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.core.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Some file related util methods.
 */
public final class ZGaeFileUtil
{
    /** Default buffer size. */
    public static final int DEFAULT_BUFFER = 1024 * 4;

    /**
     * Read the contents of the input stream and write them to the output
     * stream. Uses the default buffer size. The method returns the number of
     * bytes that copied.
     *
     * @param aIn
     * @param aOut
     * @throws IOException
     */
    public static long copy(InputStream aIn, OutputStream aOut) throws IOException
    {
        return copy(aIn, aOut, DEFAULT_BUFFER);
    }

    /**
     * Read the contents of the input stream and write them to the output
     * stream. Uses the given buffer size. Returns the number of bytes copied.
     *
     * @param aIn
     * @param aOut
     * @param aBufferSize The buffer size to use for reading.
     * @return the number of bytes copied
     * @throws IOException
     */
    public static long copy(InputStream aIn, OutputStream aOut, int aBufferSize) throws IOException
    {
        byte[] buffer = new byte[aBufferSize];
        int read = 0;
        long totalBytes = 0;
        while (-1 != (read = aIn.read(buffer)))
        {
            aOut.write(buffer, 0, read);
            totalBytes += read;
        }
        return totalBytes;
    }

    /**
     * Recursively deletes the given directory and all of its nested contents.
     *
     * @param aDirectory
     */
    public static final void recursivelyDelete(File aDirectory)
    {
        if (!aDirectory.isDirectory())
            return;

        File[] files = aDirectory.listFiles();

        if (files != null)
        {
            for (int i = 0; i < files.length; i++)
            {
                if (files[i].isFile())
                    ZGaeFileUtil.deleteFile(files[i]);
                else
                    recursivelyDelete(files[i]);
            }
        }
        ZGaeFileUtil.deleteFile(aDirectory);
    }

    /**
     * Deletes the given file.
     *
     * @param aFileOrDirectory
     */
    public static final void deleteFile(File aFileOrDirectory)
    {
        if (!aFileOrDirectory.delete())
            ZGaeUtil.logError(new RuntimeException("File not deleted: " + aFileOrDirectory));
    }

    /**
     * Make directories.
     *
     * @param aDirectory
     */
    public static final void makeDirs(File aDirectory)
    {
        if (aDirectory.isDirectory())
            return;
        if (!aDirectory.mkdirs())
            ZGaeUtil.logError(new RuntimeException("Directories not created for: " + aDirectory));
    }
}
